yjx.service('LoginService', ['$rootScope', 'ipCookie', '$location', '$state', '$timeout', '$fetch', '$init', 'modalService',  'browserJudge','$locals',
    function LoginService($rootScope, ipCookie, $location, $state, $timeout, $fetch, $init, modalService, browserJudge,$locals) {
        return {
            wxAutoLogin: function (url) {
                wxAutoLogin(url);
            },

            /*用户登录后将信息缓存至ipCookie 、$rootScope的赋值*/
            initLoginData: function(data,pCookieParams) {
                if(data) {
                    //alert(data.user.digests)
                    var cookieParams = pCookieParams || ipCookiesParam;
                    ipCookie('Uid', data.Uid, cookieParams);
                    ipCookie('user', data.user, cookieParams);
                    ipCookie('avatar', data.user.avatar, cookieParams);
                    ipCookie('nickname', data.user.nickname,cookieParams);
                    ipCookie('telephone', data.user.telephone, cookieParams);
                    ipCookie('username', data.user.userName, cookieParams);

                    $rootScope.avatar = data.user.avatar;
                    $rootScope.nickname = data.user.nickname;
                    $rootScope.telephone = data.user.telephone;
                    $rootScope.userId = data.user.id;
                    $rootScope.user = data.user;
                    $rootScope.digests = data.user.digests;
                    $rootScope.alipayAccount = data.user.alipayAccount;
                }
            },

            /*从cookie中获取初始化数据*/
            getLoginData: function(){
                $rootScope.avatar =  ipCookie('avatar');
                $rootScope.nickname =  ipCookie('nickname');
                $rootScope.telephone =  ipCookie('telephone');
                $rootScope.userId =  ipCookie('Uid');
                $rootScope.user = ipCookie('user');
                $rootScope.digests = $rootScope.user?$rootScope.user.digests:'';
                alert($rootScope.userId);
            }
        }
        function wxAutoLogin(url){
            //alert(url)
            if(url != null && url.trim() != '' && url[0] != '/') {
                 url ='/'+ url;
            }
            //登录授权后回调到选择身份登录页面
            url = url?url:''
            if (!$location.search().code && !$location.search().state && browserJudge.isWx()) {
                //alert("回调地址=============="+url);
                //alert('微信内部浏览器打开，直接验证授权');
                var firstRedirectUri = encodeURIComponent(
                    // 默认回调地址以https://开头;
                    // "https://www.aiekt.com/#/",
                    "http://www.genetuan.com/#/",
                    // URL编码格式
                    "utf-8");
                var finalRedirectUri = encodeURIComponent(
                    // 以http:开头的被作为最终回调地址(可带？参数);   url参数以‘/’开头
                    "http://www.genetuan.com/#"+url,
                    // URL编码格式
                    "utf-8");

                var wxLogin = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                        // appid
                    + "appid=wx34ebc701fdef41d5"
                        // redirect_uri
                    + "&redirect_uri=" + firstRedirectUri
                        // response_type
                    + "&response_type=code"
                        // scope
                    + "&scope=snsapi_userinfo"
                        // state 参数，以http://开头将被作为最终回调地址(可带？参数);否则回到首页
                        //+ "&state=wxlogin"
                    + "&state="+finalRedirectUri
                        // 结束标记（必传）
                    + "#wechat_redirect";
                //alert(wxLogin);
                self.location.href = wxLogin;
            }
        }


    }]);
